// pages/solarclassification/solarclassification.js
var utils = require('../../utils/util.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        currentPage:1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        that.setData({
            banner: options.banner,
            id: options.id,
            number: wx.getStorageSync("number")
         
        })
        that.goodslist(that.data.currentPage);
        that.lll();
    },
    lll: function (e) {
        var that = this;
        if (that.data.allListData != undefined) {
            var allListData = that.data.allListData;
            for (var i in allListData) {
                allListData[i].number = 0;
                for (var k in wx.getStorageSync("listPost")) {
                    if (wx.getStorageSync("listPost")[k].id == allListData[i].id) {
                        allListData[i].number = wx.getStorageSync("listPost")[k].number;
                    }
                }
            }
            that.setData({
                allListData: allListData
            })
        }
    },
    //所有的商品列表
    goodslist: function (currentPage) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/index/activity_goods", {
            activity_id: that.data.id,
            page: currentPage
        }, function (res) {
            utils.hideLoading();
            console.log(res.datas);
            var allpost = that.data.allListData;
            if (allpost == undefined) {
                allpost = [];
            }
            for (var i in res.datas) {
                res.datas[i].number = 0;
                allpost.push(res.datas[i])
            }
            if (res.datas.length < 10) {
                that.setData({
                    tishi_title: '已加载全部',
                });
            } else {
                that.setData({
                    tishi_title: '加载更多',
                });
            }
            that.setData({
                allListData: allpost,
                 ready: res.datas.length == 0 ? 1 : 2,
            })
             that.lll();
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.ready == 2) {
            var page = that.data.currentPage + 1
            that.setData({
                currentPage: page,
            })
            that.goodslist(page)
        }
    },
    //加数量
    onPrevTap: function (e) {
        var that = this;
        var itemslist = that.data.allListData;
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.number;
        itemslist[index].number = itemslist[index].number + 1;
        that.setData({
            allListData: itemslist
        })
        var shuzi = wx.getStorageSync("number") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("number", number);
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false, //购物车做删除时候用
            number: 1 //加减数量
        }
        utils.uniqueSearchRecord(dataInfo);
    },
    //减数量
    onNextTap: function (e) {
        var that = this;
        var itemslist = that.data.allListData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        if (itemslist[index].number > 0) {
            itemslist[index].number = itemslist[index].number - 1;
            var nowStorage = wx.getStorageSync("listPost");
            for (var i in nowStorage) {
                if (nowStorage[i].id == itemslist[index].id) {
                    nowStorage[i].number = nowStorage[i].number - 1;
                    if (nowStorage[i].number == 0) {
                        nowStorage.splice(i, 1);
                    }
                }
            }
            that.setData({
                number: wx.getStorageSync("number") - 1,
                allListData: itemslist
            })
            wx.setStorageSync("listPost", nowStorage)
            wx.setStorageSync("number", that.data.number);
        } else {
            utils.message("该宝贝宝贝不能减少哦~")
        }

    },
    //跳入购物车
    onCarShoppTap:function(e){
        console.log(e);
        wx.switchTab({
            url: '../cartshopp/cartshopp'
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})